(ns first-clojure.db.db-guestbook
  (:require
    [clj-time.jdbc]
    [clojure.java.jdbc :as jdbc]
    [clojure.tools.logging :as log]
    [conman.core :as conman]
    [java-time.pre-java8 :as jt]
    [first-clojure.config :refer [env]]
    [mount.core :refer [defstate]])
  (:import [java.sql
            BatchUpdateException
            PreparedStatement]))

(defstate ^:dynamic *db*
          :start ( do (Class/forName "net.sf.log4jdbc.DriverSpy")
                   (if-let [jdbc-url (env :database-url)]
                     (conman/connect! {:jdbc-url jdbc-url})
                     (do
                       (log/warn "database connection URL was not found, please set :database-url in your config, e.g: dev-config.edn")
                       *db*)))
          :stop (conman/disconnect! *db*))

(conman/bind-connection *db* "sql/guestbook.sql")
